package com.bdth.bdthiss.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.bdth.bdthiss.vo.MessageResult;
import com.bdth.bdthiss.vo.Pagination;
import com.bdth.bdthiss.entity.Rule;
import com.bdth.bdthiss.service.RuleService;
import com.bdth.bdthiss.vo.PageVo;
import com.bdth.bdthiss.vo.RuleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGP
 * @since 2018-11-13
 */
@Controller
@RequestMapping("/rule")
public class RuleController {
    @Autowired
    private RuleService ruleService;

    @RequestMapping(value = "index")
    public String index() {
        return "rule/index";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public PageVo<Rule> getPageList(Pagination<Rule> pagination, @RequestParam(required = false) String name, @RequestParam(required = false) String type, @RequestParam(required = false) String title){
        Page<Rule> items = ruleService.listPages(pagination, name, type, title);
        PageVo<Rule> pageVo = new PageVo<>(items.getRecords(),items.getTotal());
        return pageVo;
    }

    @RequestMapping(value = "form")
    public String form(){
        return "rule/form";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public MessageResult save(RuleVo param){
        Rule bean = new Rule();
        bean.setName(param.getName());
        bean.setTitle(param.getTitle());
        bean.setType(param.getType());
        bean.setStatus(param.getStatus());
        bean.setCrtime(new Date());
        ruleService.insert(bean);
        return MessageResult.getSuccess("添加数据成功");
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public MessageResult update(RuleVo param){
        Rule bean = new Rule();
        bean.setName(param.getName());
        bean.setTitle(param.getTitle());
        bean.setType(param.getType());
        bean.setStatus(param.getStatus());
        bean.setId(param.getId());
        bean.setUptime(new Date());
        ruleService.updateById(bean);
        return MessageResult.getSuccess("修改数据成功");
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public MessageResult delete(@RequestParam Integer ids){
        ruleService.deleteById(ids);
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "remove")
    @ResponseBody
    public MessageResult deletes(@RequestParam String ids){
        String[] id = ids.split(",");
        for (String i: id) {
            Integer intId = Integer.parseInt(i);
            ruleService.deleteById(intId);
        }
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "search")
    public String search(){
        return "rule/search";
    }
}

